<?php

/**
 * 登录
 * Date: 2018/11/21
 */

namespace Lib\Paas;

use Lib\Base\Request;
use Lib\Base\Validate;
use Lib\Cache\RedisAction;

class Login extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 检查登陆状态
     * @return bool true 登录成功，false 登录失败
     */
    public function checkLogin()
    {
        if (empty($this->getLoginInfo())) {
            recordLog("登录态校验失败，获取登录态相关信息为empty。", __FILE__, __LINE__);
            return false;
        }

        //组合URL
        $url = $this->getPaasRequestUrl('user.checklogin');
        $res = $this->http->get($url);
        recordLog("ulink paas login return data：" . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != '0') {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'user.checklogin');
            recordLog('调用ULink Paas校验用户登录态时出现异常！', __FILE__, __LINE__, LP_ERROR);
            return false;
        }
        $res = $res['data'];

        //['ret'=>'0-接口调用成功，非0-接口调用失败','is_login'=>'1-登录，0-未登录']
        if ($res['ret'] != '0') {
            return false;
        }

        if ($res['data']['ret'] == '0' && $res['data']['is_login'] == '1') {
            if (empty($res['data']['gameOpenid'])) {
                recordLog("login success, gameOpenid empty!", __FILE__, __LINE__, LP_ERROR);
                return false;
            }
            Request::setParam(GAME_OPENID, $res['data']['gameOpenid']);

            Request::setGetParam('iBaseUin', $res['data']['iBaseUin']);
            Request::setPostParam('iBaseUin', $res['data']['iBaseUin']);
            Request::setCookieParam('iBaseUin', null);
            //$_COOKIE['iBaseUin'] = $res['data']['iBaseUin'];
            return true;
        } else {
            return false;
        }
    }

    /**
     * openid 转换，将Ulink授权获得openid转换成业务对应的openid
     * @param string $sTargetAppid 待转换业务的appid
     * @return bool|string 成功-返回转换之后的openid， 失败-false
     */
    public function translateOpenid($sTargetAppid = '')
    {
        //自定义目标appid
        $paasParam = ['sTargetAppid' => $sTargetAppid];

        //组合URL
        $url = $this->getPaasRequestUrl('user.getopenid', $paasParam);
        $res = $this->http->get($url);
        recordLog("ulink paas translateOpenid return data：" . json_encode($res), __FILE__, __LINE__);

        if ($res['ret'] != '0') {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'user.getopenid');
            recordLog('调用ULink Paas转换用户openid时出现异常！', __FILE__, __LINE__, LP_ERROR);
            return false;
        }
        $res = $res['data'];

        if (is_array($res) && $res['ret'] == '0' && isset($res['data']['openid'])) {
            return $res['data']['openid'];
        } else {
            return false;
        }
    }

    /**
     * 检查指定openid是否在白名单
     * @param string $openid【选填】 带检查的openid，如果不传，则判断当前登录态中的openid转换为游戏业务的openid是否在白名单
     * @return bool true-在白名单， false-不在白名单
     */
    public function checkInWhite($openid = '')
    {
        if ($openid == '') {
            $openid = !empty(Request::param('openid')) ? Request::param('openid') : $this->translateOpenid();
        }
        if ($openid === false) {
            return false;
        } else {
            //从redis集合中判断是否在白名单
            $actId = Request::param('iActId');
            $redis = new RedisAction(['instance' => 'ulink']);
            if (!Validate::checkNaturalNum($actId)) {
                recordLog("actid error，actid=" . $actId, __FILE__, __LINE__, LP_ERROR);
                return false;
            }
            $key = 'whitelist';
            return $redis->sIsMember($key, $openid);
        }
    }
}
